package com.hk.yajin.function.start;

import android.animation.ValueAnimator;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;

import com.airbnb.lottie.LottieAnimationView;
import com.hk.developmentlib.utills.CacheUtils;
import com.hk.developmentlib.utills.PreferUtil;
import com.hk.developmentlib.utills.constants.PreferConstant;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.MainActivity;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * 类描述：启动页的Activity
 * 创建人：huj
 * 创建时间：2016/4/19 9:43
 */
public class LaunchActivity extends BaseActivity {

    //启动页停留的时间
    // private long stopTime = 2000;
    // MyRunnable myRunnable = new MyRunnable();
    private final int SDK_PERMISSION_REQUEST = 100;
    LottieAnimationView animationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        requiresMainPermission();
    }

//
//
//    protected void requestmMainFunctionPermisson() {
//        if (EasyPermissions.hasPermissions(this, permissions[0], permissions[1], permissions[2])) {
//            UiUtil.postDelayed(myRunnable, stopTime);
//        } else {
//            EasyPermissions.requestPermissions(this, "为使APP正常运行，请允许以下权限", 100, permissions[0], permissions[1], permissions[2]);
//        }
//
//    }

    @AfterPermissionGranted(SDK_PERMISSION_REQUEST)
    private void requiresMainPermission() {
        if (EasyPermissions.hasPermissions(this, permissions[0], permissions[1], permissions[2])) {
            CacheUtils.getInstance();//获取权限之后再去初始化初存储器!
            //UiUtil.postDelayed(myRunnable, stopTime);
            startLottieLogoAnimation();
        } else {
            EasyPermissions.requestPermissions(this, getString(R.string.permisson_tip1), SDK_PERMISSION_REQUEST, permissions[0], permissions[1], permissions[2]);
        }
    }

    private void startLottieLogoAnimation() {
//        animationView = (LottieAnimationView) findViewById(R.id.animationView);
//        animationView.setProgress(0f);
//        animationView.setAnimation("startanimation.json", LottieAnimationView.CacheStrategy.Strong);
//        animationView.setImageAssetsFolder("lottie");
//        animationView.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//
//            }
//        });
//        animationView.loop(false);
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(getAnimationDuraing());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
               // animationView.setProgress(animation.getAnimatedFraction());
                if (animation.getAnimatedFraction() == 1) {
                    //第一次启动应用,跳转到启动页
                    if (!PreferUtil.getBoolean(PreferConstant.FIRST_USE, false)) {
                        Intent intent = new Intent(LaunchActivity.this, GuideActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    finish();
                }
            }
        });
        animator.start();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, list)) {
            new AppSettingsDialog.Builder(this)
                    .setTitle(R.string.getpermisson_faild)
                    .setPositiveButton(R.string.go_setting)
                    .setRationale(R.string.permisson_tip2)
                    .setNegativeButton(getString(R.string.qiut), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    }).build().show();
        } else {
            requiresMainPermission();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            requiresMainPermission();
        }
    }


    public class MyRunnable implements Runnable {
        @Override
        public void run() {
            //第一次启动应用,跳转到启动页
            if (!PreferUtil.getBoolean(PreferConstant.FIRST_USE, false)) {
                Intent intent = new Intent(LaunchActivity.this, GuideActivity.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
                startActivity(intent);
            }
            finish();
        }
    }

    @Override
    public void onBackPressed() {

    }

    private long getAnimationDuraing() {
        if (!PreferUtil.getBoolean(PreferConstant.FIRST_USE, false)) {
            return 5000;
        } else {
            return 3000;
        }
    }

}
